import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

/// 单选框（两个选项按钮）
/// [selectedState]:单选框选中状态
/// [leftText]:开关左侧文案
/// [rightText]:开关右侧文案
/// [checkListener]:开关点击回调
class CustomRadioButtonWidget extends StatefulWidget {
  final RadioButtonSelectedState selectedState;
  final String leftText;
  final String rightText;
  final Function checkListener;

  CustomRadioButtonWidget(
      {this.selectedState = RadioButtonSelectedState.None,
      this.leftText = "有",
      this.rightText = "无",
      this.checkListener});

  @override
  State<StatefulWidget> createState() {
    return _CustomRadioButtonWidgetState(selectedState);
  }
}

class _CustomRadioButtonWidgetState extends State<CustomRadioButtonWidget> {
  _CustomRadioButtonWidgetState(this.selectedState);

  final TextStyle selectedStyle = TextStyle(color: Color(0xFF333333));
  final TextStyle unSelectedStyle = TextStyle(color: Color(0xFF999999));
  RadioButtonSelectedState selectedState;

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 48,
      color: Colors.white,
      child: Row(
        children: <Widget>[
          Container(
              height: 48,
              child: GestureDetector(
                behavior: HitTestBehavior.opaque,
                onTap: () {
                  setState(() {
                    selectedState = RadioButtonSelectedState.Left;
                  });
                  if (widget.checkListener != null) {
                    widget.checkListener(RadioButtonSelectedState.Left);
                  }
                },
                child: Row(
                  children: <Widget>[
                    SizedBox(
                      width: 9,
                    ),
                    Image(
                      image: AssetImage(
                        selectedState == RadioButtonSelectedState.Left
                            ? "packages/flutter_dk_form/images/dk_form/selected.png"
                            : "packages/flutter_dk_form/images/dk_form/unselected.png",
                      ),
                      width: 14,
                      height: 14,
                      fit: BoxFit.scaleDown,
                    ),
                    SizedBox(
                      width: 9,
                    ),
                    Text(
                      widget.leftText,
                      style: selectedState == RadioButtonSelectedState.Left
                          ? selectedStyle
                          : unSelectedStyle,
                    ),
                    SizedBox(
                      width: 13,
                    ),
                  ],
                ),
              )),
          Container(
              height: 48,
              child: GestureDetector(
                behavior: HitTestBehavior.opaque,
                onTap: () {
                  setState(() {
                    selectedState = RadioButtonSelectedState.Right;
                  });
                  if (widget.checkListener != null) {
                    widget.checkListener(RadioButtonSelectedState.Right);
                  }
                },
                child: Row(
                  children: <Widget>[
                    SizedBox(
                      width: 9,
                    ),
                    Image(
                      image: AssetImage(
                        selectedState == RadioButtonSelectedState.Right
                            ? "packages/flutter_dk_form/images/dk_form/selected.png"
                            : "packages/flutter_dk_form/images/dk_form/unselected.png",
                      ),
                      width: 14,
                      height: 14,
                      fit: BoxFit.scaleDown,
                    ),
                    SizedBox(
                      width: 9,
                    ),
                    Text(
                      widget.rightText,
                      style: selectedState == RadioButtonSelectedState.Right
                          ? selectedStyle
                          : unSelectedStyle,
                    ),
                    SizedBox(
                      width: 16,
                    ),
                  ],
                ),
              )),
        ],
      ),
    );
  }
}

enum RadioButtonSelectedState {
  ///无选中
  None,
  ///左侧按钮选中
  Left,
  ///右侧按钮选中
  Right
}


